package io.github.harmonly.service.impl;

import io.github.harmonly.entity.Apply;
import io.github.harmonly.mapper.ApplyMapper;
import io.github.harmonly.mapper.UserMapper;
import io.github.harmonly.service.ApplyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ApplyServiceImpl implements ApplyService {

    @Resource
    private ApplyMapper mapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public List<Apply> getAppliesBySender(int uid) {
        return mapper.getAppliesBySender(uid);
    }

    @Override
    public List<Apply> getAppliesByReceiver(int uid) {
        return mapper.getAppliesByReceiver(uid);
    }

    @Override
    public Apply getApply(int apid) {
        return mapper.getApply(apid);
    }

    @Override
    public List<Apply> getApplies() {
        return mapper.getApplies();
    }

    @Override
    public boolean addApply(Apply apply) {
        if (mapper.isExist(apply) != null) return false;
        if (userMapper.getUser(apply.getReceiver()) != null) return mapper.addApplyToReceiver(apply) == 1;
        return mapper.addApply(apply) == 1;
    }

    @Override
    public boolean deleteApply(int apid) {
        return mapper.deleteApply(apid) == 1;
    }

    @Override
    public boolean editApply(Apply apply) {
        return mapper.editApply(apply) == 1;
    }
}
